<template>
  <div>
    <el-autocomplete
      v-if="itemtype == 1"
      placeholder="请输入车牌编号"
      v-model="choosedp"
      :fetchSuggestions="searchjbr"
      clearable
      @select="selectjbr"
      @clear="clearchoosedp"
      style="width:100%;"
    ></el-autocomplete>
    <el-autocomplete
      v-else
      placeholder="请输入牌照"
      v-model="choosedp"
      :fetchSuggestions="searchjbr"
      @select="selectjbr"
      clearable
      style="width:100%;"
      @clear="clearchoosedp"
    ></el-autocomplete>
  </div>
</template>

<script>
export default {
  props: {
    itempersonid: String,
    itemtype: Number,
    careditid: String,
  },
  data() {
    return {
      ts: this.newStore(),
      jbrList: [],
      choosedp: '',
    };
  },
  created() {
    this.getjbrList();
    if (this.careditid != '' || this.careditid != undefined) {
      this.choosedp = this.careditid;
    }
  },
  methods: {
    clearchoosedp() {
      this.$emit('getpersonid2', { id: '', id2: '', value: '' });
    },
    // 获取经办人列表
    getjbrList() {
      this.jbrList = [];
      if (this.itemtype == 1) {
        const req = {
          handle: '0E56125B90D2898A62E387C1CED62EC408B04B35BD44D675',
          query: '',
        };
        this.spost(this.ts, '/mainservlet', req).then((ts) => {
          for (let index = 0; index < ts.d.data.operation1.length; index += 1) {
            const a = {};
            a.value = ts.d.data.operation1[index].hash.carid;
            a.id = ts.d.data.operation1[index].hash.carid;
            a.id2 = ts.d.data.operation1[index].hash.carno;
            this.jbrList.push(a);
          }
          if (this.itempersonid) {
            for (let i = 0; i < this.jbrList.length; i += 1) {
              if (this.itempersonid) {
                console.log(this.jbrList[i]);

                if (this.itempersonid == this.jbrList[i].id) {
                  this.choosedp = this.jbrList[i].value;
                }
              }
            }
          }
        });
      } else if (this.itemtype == 2) {
        const req = {
          handle: '0E56125B90D2898A62E387C1CED62EC408B04B35BD44D675',
          query: '',
        };
        this.spost(this.ts, '/mainservlet', req).then((ts) => {
          ts.d.data.operation1.unshift({
            cols: ['carid', 'carno'],
            hash: { carno: '无', carid: '无' },
          });
          for (let index = 0; index < ts.d.data.operation1.length; index += 1) {
            const a = {};
            a.value = ts.d.data.operation1[index].hash.carno;
            a.id = ts.d.data.operation1[index].hash.carno;
            a.id2 = ts.d.data.operation1[index].hash.carid;
            this.jbrList.push(a);
          }
          if (this.itempersonid) {
            for (let i = 0; i < this.jbrList.length; i += 1) {
              if (this.itempersonid) {
                if (this.itempersonid == this.jbrList[i].id2) {
                  this.choosedp = this.jbrList[i].value;
                }
              }
            }
          }
        });
      }
    },
    // 搜索经办人
    searchjbr(queryString, cb) {
      const results = queryString
        ? this.jbrList.filter(this.createStateFilter(queryString))
        : this.jbrList;

      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 1000 * Math.random());
    },
    createStateFilter(queryString) {
      return jbrList => jbrList.value.indexOf(queryString) > -1;
    },
    // 选择经办人后
    selectjbr(val) {
      console.log(val, 'val');

      this.$emit('getpersonid2', val);
    },
  },
};
</script>

<style>
</style>
